<?php
/***********************************************************
    [EasyTalk] (C)2009 - 2011 nextsns.com
    This is NOT a freeware, use is subject to license terms

    @Filename GuideAction.class.php $

    @Author hjoeson $

    @Date 2011-01-09 08:45:20 $
*************************************************************/

class GuideAction extends Action {

    public function _initialize() {
        parent::init();
        A('Api')->tologin();
        if ($this->my['userguide']==1) {
            header('location:'.SITE_URL);
            exit;
        }
    }

    public function index() {
//        $province=D('District')->where('level=1')->select();
//        $this->assign('province',$province);
//        $this->display();

// added 7/5/2011 Luozhengfeng yze
//header("Content-Type:text/html; charset=utf-8");
		$Province=M('region');
		$map['parent_id']=1;
		$result=$Province->where($map)->findAll();
		//echo json_encode($result);
		$this->assign('vol',$result);
		$this->assign('hvol',$result);
		//dump($result);
		$this->display();
    }

	public function ajaxData(){
		$map['parent_id']=$_POST['pid'];//获取post传递的pid参数
		$Province=M('region');//实例化
		$result=$Province->where($map)->findAll();//带入参数查询
		if(!empty($result)){//判断是否有结果
			$this->ajaxReturn($result,'ok',1);//如果有结果 返回一个json格式的数据
		}else{
			$this->ajaxReturn(0,'error',0);//如果没有返回空
		}
	}
	public function ajaxData_fuwu(){
		$map['parent_id']=$_POST['pid'];//获取post传递的pid参数
		$fuwufenlei=M('skill');//实例化
		$result=$fuwufenlei->where($map)->findAll();//带入参数查询
		if(!empty($result)){//判断是否有结果
			$this->ajaxReturn($result,'ok',1);//如果有结果 返回一个json格式的数据
		}else{
			$this->ajaxReturn(0,'error',0);//如果没有返回空
		}
	}

public function doset() {
        $user=M('Users');
        $ctent=D('Content');
        $data=array();

		$city_code=$_POST['lone'].' '.$_POST['ltwo'].' '.$_POST['lthree'].' '.$_POST['hone'].' '.$_POST['htwo'].' '.$_POST['hthree'];

		// get the live and home's sheng/city/xian 's address
		$mapls['region_id']=$_POST["lone"];
		$maplc['region_id']=$_POST["ltwo"];
		$maplx['region_id']=$_POST["lthree"];

		$maphs['region_id']=$_POST["hone"];
		$maphc['region_id']=$_POST["htwo"];
		$maphx['region_id']=$_POST["hthree"];

		$Province=M('region');
		$lshenfeng=trim($Province->where($mapls)->getField('region_name'));
		$lcity=trim($Province->where($maplc)->getField('region_name'));
		$lxian=trim($Province->where($maplx)->getField('region_name'));

		$hshenfeng=trim($Province->where($maphs)->getField('region_name'));
		$hcity=trim($Province->where($maphc)->getField('region_name'));
		$hxian=trim($Province->where($maphx)->getField('region_name'));
	

        //$city=trim($_POST["livesf"].' '.$_POST["livecity"]);
		$live_city=trim($lshenfeng.' '.$lcity.' '.$lxian);
		$home_city=trim($hshenfeng.' '.$hcity.' '.$hxian);
		
		$userqq=$_POST['userqq'];
		$userweibo=$_POST['userweibo'];

        $nickname= daddslashes(clean_html(trim($_POST["nickname"])));
        $gender= $_POST["gender"];
        //$city=trim($_POST["livesf"].' '.$_POST["livecity"]);
        $info= daddslashes($ctent->replace(trim($_POST["info"])));

        if(!preg_match('/^[0-9a-zA-Z\xe0-\xef\x80-\xbf._-]+$/i',$nickname)) {
            Cookie::set('setok','setting2');
            header('location:'.SITE_URL.'/Guide');
            exit;
        }
        if (!$nickname && !$gender && !$live_city) {
            Cookie::set('setok','setting1');
            header('location:'.SITE_URL.'/Guide');
            exit;
        }
		if($live_city && $live_city!=$this->my['live_city']){
		    $data['live_city']=$live_city;
		}
		if($home_city && $home_city!=$this->my['home_city']){
		    $data['home_city']=$home_city;
		}
		if($city_code && $city_code!=$this->my['city_code']){
			$data['city_code']=$city_code;
		}
        if ($live_city && $live_city!=$this->my['live_city'] && $live_city!="选择省份"){
            $data['live_city']=$live_city;
        }
		if ($home_city && $home_city!=$this->my['home_city'] && $home_city!="选择省份"){
            $data['home_city']=$home_city;
        }
//        if ($city && $city!=$this->my['live_city'] && $city!="选择省份 选择城市"){
//            $data['live_city']=$city;
//        }

        if ($gender!=$this->my['user_gender']){
            $data['user_gender']=$gender;
        }
		if ($userqq!=$this->my['userqq']){
            $data['userqq']=$userqq;
        }
		if ($userweibo!=$this->my['userweibo']){
            $data['userweibo']=$userweibo;
        }
        if ($info!=$this->my['user_info']){
            $data['user_info']=$info;
        }
        if ($nickname && $nickname!=$this->my['nickname'] && $this->my['user_auth']==0) {
            if (StrLenW($nickname)<=12 && StrLenW($nickname)>=1) {
                $newnickname=$user->where("user_name='$nickname' OR nickname='$nickname'")->find();
                if ($newnickname) {
                    Cookie::set('setok','setting4');
                    header('location:'.SITE_URL.'/Guide');
                    exit;
                } else {
                    $data['nickname']=$nickname;
                    //atuserlist
                    D('Atusers')->where("atuname='".$this->my['user_name']."'")->setField('atnickname',$nickname);
                }
            } else {
                Cookie::set('setok','setting2');
                header('location:'.SITE_URL.'/Guide');
                exit;
            }
        }
        $user->where("user_id='".$this->my['user_id']."'")->data($data)->save();
        Cookie::set('setok','setting3');
        header('location:'.SITE_URL.'/Guide/fuwu');
    }

	public function fuwu(){
		$fuwufenlei=M('skill');
		$map['parent_id']=1;
		$result_fuwufenglei=$fuwufenlei->where($map)->findAll();
		$this->assign('vol_fuwu',$result_fuwufenglei);

		$Province=M('region');
		$map['parent_id']=1;
		$result_qinyous=$Province->where($map)->findAll();
		//echo json_encode($result);
		$this->assign('vol',$result_qinyous);


		//把服务信息发送到fuwu.htm文件，显示此用户自己提供的服务信息汇总。
		$fuwu=M('fuwu');
		$result_fuwu=$fuwu->where("user_id='".$this->my['user_id']."'")->findall();
		$this->assign('volfuwulist',$result_fuwu);
//		$result_fuwuid=$fuwu->where("user_id='".$this->my['user_id']."'")->getField('fuwuid');
//		$this->assign('volfuwuid',$result_fuwuid);
        $this->display();
	}

 public function jia_fuwu(){
		$fuwuu=M('fuwu');
		$fuwufenlei=M('skill');
		$Province=M('region');
		$ctent=D('Content'); //这个地方我担心是否会有性能问题？是否没有必要实例化一个Content？ 5/24/2011 luozhengfeng -->6/27/2011 有必要
		$data=array();
		 $mapfuwulastid['user_id']=$this->my['user_id'];
		 $data['user_id']= $this->my['user_id'];
		 $data['user_name']= $this->my['user_name'];
		 $data['fuleibie']=trim($_POST['fufenlei']);		 
		 $data['zileibie']=trim($_POST['zifenlei']);
		 if($_POST['zifenlei']==-1){
					Cookie::set('setok','fuwu_leibieming_need');
					header('location:'.SITE_URL.'/Setting');
					exit;
				}

		 $mapfufenlei['skill_id']=$_POST['fufenlei'];
		 $mapzifenlei['skill_id']=$_POST['zifenlei'];

		$fleibieming=$fuwufenlei->where($mapfufenlei)->getField('skill_name');
		$zleibieming=$fuwufenlei->where($mapzifenlei)->getField('skill_name');
		$fuwuleibieming=$fleibieming.'-'.$zleibieming;

	     $data['fuwuleibieming']=$fuwuleibieming;

		 $fuwubiaoti= daddslashes(clean_html(trim($_POST["fuwubiaoti"])));
		 if (!$fuwubiaoti) {
            Cookie::set('setok','setting5');
            header('location:'.SITE_URL.'/Guide/fuwu');
            exit;
        }
		 $data['fuwubiaoti']=$fuwubiaoti;
		 $data['fuwujiage']=trim($_POST['fuwujiage']);
		 $data['yuerdanbao']=trim($_POST['yuerdanbao']);
		 $fuwuxiangqing= daddslashes($ctent->replace(trim($_POST["fuwuxiangqing"])));
		 $data['fuwuxiangqing']=$fuwuxiangqing;
		 $zidingyiwenti= daddslashes(clean_html(trim($_POST["zidingyiwenti"])));
		 $data['zidingyiwenti']=$zidingyiwenti;
		 if($_POST['qinyou']){ 
				 //where("id='$val[topic_id]'")
		 		 $data['shifouweiqinyou']=$_POST['qinyou'];		
				 $data['hewodeganxi']=trim($_POST['hewodeganxi']);		
				 $data['suozaidizhi']=$_POST['lone'].' '.$_POST['ltwo'].' '.$_POST['lthree'];	
				 $data['suozaidizhi_ming']=$Province->where("region_id='$_POST[lone]'")->getField('region_name').' '.$Province->where("region_id='$_POST[ltwo]'")->getField('region_name').' '.$Province->where("region_id='$_POST[lthree]'")->getField('region_name');
		 }
		 $data['fuwuzhuangtai']=TRUE;
		 $data['fuwujianlishijian']=time();
		 $data['fuwuxiugaishijian']=time();
         $list=$fuwuu->data($data)->add();
		 if($list!==false){
			  //把发布的服务存放一份到content表中.6/21/2011 Luozhengfeng
			  unset($data);
			  M('Users')->setInc('fuwuzongshu','user_id='.$this->my['user_id'],1); 
			  $list_s=$fuwuu->where("user_id='".$this->my['user_id']."'")->order('fuwujianlishijian desc')->find(); 
			  $this->assign('list_s',$list_s);
			  if($list_s['shifouweiqinyou']){
				$content_body.=$list_s['user_name']."的".$list_s['hewodeganxi'];
			  }else{
					$content_body.="";
				}
			  $content_body.=" #发布了服务#: ".$list_s['fuwubiaoti']." 单价: ".$list_s['fuwujiage']." 详情: ";
			  //$content_body.=" 发布了服务: ".$list_s['fuwubiaoti']." 价格: ".$list_s['fuwujiage']."元/次或/小时  详情: ";
			  $content_body.=$list_s['fuwuxiangqing'];
			  $data['content_body']=$content_body;
			  $data['fuwuid']=$list_s['fuwuid'];
			  $data['user_id']=$list_s['user_id'];
			  $data['posttime']=time();
			  $data['filetype']="fuwu";
			  //echo $list_s['fuwuid']." time: ".time();
			  $list_ctent=$ctent->data($data)->add();
			  M('Users')->setInc('msg_num','user_id='.$this->my['user_id'],1); 
			  Cookie::set('setok','setting_fuwuok');
			  //$this->success('数据保存成功！');
			}else{
			  Cookie::set('setok','setting_fuwuno');
			  //$this->error('数据保存失败！');
			}
         header('location:'.SITE_URL.'/Guide/fuwu');
	}



    public function followtopic() {
        $topic = D('Topic')->where("topictimes>0 AND tuijian=1")->order("topictimes DESC")->limit(30)->select();
        $this->assign('topic',$topic);
        $this->display();
    }

    public function dotopic() {
        $topicnames=$_POST['topicnames'];
        if (is_array($topicnames)) {
            $keyword=implode("','",$topicnames);
            $mt=D('Mytopic');
            $tp=D('Topic');

            $tpdata=$tp->where("topicname IN ('$keyword')")->select();
            if ($tpdata) {
                foreach($tpdata as $val) {
                    $keywords[$val['id']]=$val['topicname'];
                }
                $keyword=implode("','",$keywords);
                $data=$mt->where("topic IN ('$keyword') AND user_id='".$this->my['user_id']."'")->select();
                foreach($data as $val) {
                    $followed[]=$val['topic'];
                }
                if ($followed) {
                    $diff=array_diff($keywords,$followed);
                } else {
                    $diff=$keywords;
                }
                if (is_array($diff)) {
                    foreach($diff as $val) {
                        $insert['topic']=$val;
                        $insert['user_id']=$this->my['user_id'];
                        $mt->add($insert);
                        $key=array_keys($keywords,$val);
                        $key=$key[0];
                        $tp->where("id='$key'")->setInc('follownum');
                    }
                }
            }
        }
        header('location:'.SITE_URL.'/Guide/followuser');
    }

    public function followuser() {
        $uModel=D('Users');

        //获取我已经收听的用户
        $mf=array();
        $userf=D('friend')->field('fid_jieshou')->where("fid_fasong='".$this->my['user_id']."'")->select();
        foreach($userf as $val) {
            $mf[]=$val['fid_jieshou'];
        }
        $mf=implode(',',$mf);
        $mf=$mf?$mf.','.$this->my['user_id']:$this->my['user_id'];

        $user=$uModel->where('user_id not in ('.$mf.') AND msg_num>0')->order('msg_num DESC')->limit(21)->select();

        $this->assign('user',$user);
        $this->display();
    }

    public function douser() {
        $usernames=$_POST['usernames'];
        if (is_array($usernames)) {
            $fModel=D('Friend');
            foreach($usernames as $val) {
                $fModel->addfollow($val,$this->my['user_id']);
            }
			
        }
		D('Friend')->addfollow('youzie',$this->my['user_id']);
        D('Users')->where("user_id='".$this->my['user_id']."'")->setField('userguide',1);
        header('location:'.SITE_URL);
    }

    public function doface() {
        import("@.ORG.UploadFile");
        $uploadurl='/Public/attachments/head/temp/';
        $uploadurl2=__PUBLIC__.'/attachments/head/temp/';
        $upload = new UploadFile();
        $upload->maxSize  = 2097152 ;
        $upload->allowExts  = explode(',','jpg,gif,png,jpeg');
        $upload->savePath =  ET_ROOT.$uploadurl;
        $upload->thumb =  false;
        $upload->autoSub = false;
        $upload->saveRule = time;
        $upload->thumbRemoveOrigin = true;
        if($upload->upload()) {
            $uploadList = $upload->getUploadFileInfo();
            $picinfo=getimagesize(ET_ROOT.$uploadurl.$uploadList[0]['savename']);
            echo '<script language="Javascript">parent.document.getElementById("cropbox").src="'.$uploadurl2.$uploadList[0]['savename'].'";parent.document.getElementById("imgpath").value="'.$uploadurl.$uploadList[0]['savename'].'";parent.document.getElementById("cropboxdiv").style.display="block";parent.document.getElementById("loadpic").style.display="none";parent.frames.faceinit("'.$picinfo[0].'");parent.document.getElementById("picpath").innerHTML="'.L('head_filename').$uploadList[0]['savename'].'";</script>';
        } else {
            echo '<script language="Javascript">parent.document.getElementById("loadpic").style.display="none";alert("'.L('head_upload_error').'");</script>';
        }
    }

    public function doface2() {
        $ysw=$_POST['ysw'];
        if ($ysw>460) {
            $zoom=intval($ysw)/460;
        } else {
            $zoom=1;
        }
        $x=$_POST['x']*$zoom;
        $y=$_POST['y']*$zoom;
        $w=$_POST['w']*$zoom;
        $h=$_POST['h']*$zoom;
        $imgpath=ET_ROOT.$_POST['imgpath'];
        $ext=strtolower(getExtensionName($imgpath));

        import("@.ORG.IoHandler");
        $IoHandler = new IoHandler();
        if($ext!='jpg' && $ext!='jpeg' && $ext!='gif' && $ext!='png') {
            $IoHandler->DeleteFile($imgpath);
            Cookie::set('setok','face2');
            header('location:'.SITE_URL.'/Guide');
            exit;
        }
        $image_path = ET_ROOT.'/Public/attachments/head/'.date('Ymd').'/';
        if(!is_dir($image_path)) {
            mkdir($image_path);
        }
        $f=date('His');
        //大图片
        $filename=$f.'_big.'.$ext;
        $dst_file = $image_path.$filename;
        $make_result = makethumb($imgpath,$dst_file,max(10,min(120,$w)),max(10,min(120,$h)),0,0,$x,$y,$w,$h);
        //小图片
        $filename=$f.'_small.'.$ext;
        $dst_file = $image_path.$filename;
        $make_result = makethumb($imgpath,$dst_file,max(10,min(50,$w)),max(10,min(50,$h)),0,0,$x,$y,$w,$h);
        $IoHandler->DeleteFile($imgpath);

        $user=M('Users');
        $data['user_head']=date('Ymd').'/'.$filename;
        $user->where("user_id='".$this->my['user_id']."'")->data($data)->save();
        Cookie::set('setok','face1');
        header('location:'.SITE_URL.'/Guide');
    }
}
?>